import tkinter as tk
# import threading
import requests
import json

def requestData(itemId='569339631822'):
    # 写入txt
    file = open("a.txt", "a")
    # 判断id合法性
    if itemId.isdecimal()==False|len(itemId)==0:
        return
    # 网络请求会话
    for i in range(1,5):
        requstSession = requests.session()
        url = "https://rate.taobao.com/feedRateList.htm?auctionNumId=%s&currentPageNum=%d" % (itemId,i)
        result = requstSession.get(url).content
        newResult = result.decode().strip().lstrip('(').rstrip(')')
        comments = json.loads(newResult)['comments']
        for obj in comments:
            file.write(obj['date'] + '  ' + obj['auction']['sku'] + '  ' + obj['content'] + '\n')
    print('hello world!')
    file.close()
    return

window = tk.Tk()#窗口对象
window.title('淘宝评论截取器')
window.geometry('300x150')

l = tk.Label(window,text='输入淘宝商品ID',bg='white',font=('Arial,12'),width=15,height=3)
l.pack()

textField = tk.Entry(window)
textField.pack()

b = tk.Button(window,text='生成txt',command = lambda :requestData(textField.get()))#函数有参数的情况用lambda中介函数
b.pack()




# canvas = tk.Canvas(window,bg='orange',height=100,width=100)
# text = canvas.create_text(0,0,anchor='n',text=' cao')
# canvas.place(x=0,y=0)
# # canvas.delete(text)
# def move():
#     canvas.move(text,0,1)
#     global timer
#     timer = threading.Timer(0.02,move)
#     timer.start()
# timer = threading.Timer(0.02,move)
# timer.start()
# requestData();

window.mainloop()
